<template>
  <div class="app-container">
    <div class="page-header">
      <h2>救援任务</h2>
    </div>
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="订单编号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="服务内容" prop="serviceContent">
        <el-input
          v-model="queryParams.serviceContent"
          placeholder="请输入服务内容"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="评价星级" prop="evaluateLevel">
        <el-select v-model="queryParams.evaluateLevel" placeholder="请选择评价星级" clearable>
          <el-option label="全部" value="" />
          <el-option label="1星" value="1" />
          <el-option label="2星" value="2" />
          <el-option label="3星" value="3" />
          <el-option label="4星" value="4" />
          <el-option label="5星" value="5" />
        </el-select>
      </el-form-item>
      <el-form-item label="评价时间" prop="evaluateTime">
        <el-date-picker
          v-model="queryParams.evaluateTime"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          value-format="YYYY-MM-DD HH:mm:ss"
          clearable
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">筛选</el-button>
        <el-button icon="Refresh" @click="resetQuery">清空筛选条件</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:rescue:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:rescue:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:rescue:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:rescue:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="rescueList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="求助人" align="center" prop="name">
        <template #default="scope">
          <div>
            <div>{{ scope.row.name }}</div>
            <div style="color: #999; font-size: 12px;">{{ scope.row.phone }}</div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="救援类型" align="center" prop="typeId">
        <template #default="scope">
          <el-tag v-if="scope.row.typeId === 1" type="success">搭电</el-tag>
          <el-tag v-else-if="scope.row.typeId === 2" type="warning">换胎</el-tag>
          <el-tag v-else-if="scope.row.typeId === 3" type="danger">拖车</el-tag>
          <el-tag v-else-if="scope.row.typeId === 4" type="info">空运</el-tag>
          <span v-else>{{ scope.row.typeId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="车牌" align="center" prop="carNum">
        <template #default="scope">
          <span v-if="scope.row.evaluateLevel">{{ scope.row.evaluateLevel }}星</span>
          <span v-else>{{ scope.row.carNum }}</span>
        </template>
      </el-table-column>
      <el-table-column label="救援位置" align="center" prop="position" />
      <el-table-column label="终点位置" align="center" prop="endPosition" />
      <el-table-column label="申请时间" align="center" prop="applyTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.applyTime, '{y}.{m}.{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:rescue:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:rescue:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <div class="pagination-container">
      <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
      <div class="total-info">
        共{{ total }}条
      </div>
    </div>

    <!-- 添加或修改救援任务对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="rescueRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户id" prop="uid">
          <el-input v-model="form.uid" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="救援类型id" prop="typeId">
          <el-input v-model="form.typeId" placeholder="请输入救援类型id" />
        </el-form-item>
        <el-form-item label="位置" prop="position">
          <el-input v-model="form.position" placeholder="请输入位置" />
        </el-form-item>
        <el-form-item label="终点位置" prop="endPosition">
          <el-input v-model="form.endPosition" placeholder="请输入终点位置" />
        </el-form-item>
        <el-form-item label="申请时间" prop="applyTime">
          <el-date-picker clearable
            v-model="form.applyTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择申请时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="求助人" prop="name">
          <el-input v-model="form.name" placeholder="请输入求助人" />
        </el-form-item>
        <el-form-item label="联系方式" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系方式" />
        </el-form-item>
        <el-form-item label="车牌号" prop="carNum">
          <el-input v-model="form.carNum" placeholder="请输入车牌号" />
        </el-form-item>
        <el-form-item label="求助信息描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入求助信息描述" />
        </el-form-item>
        <el-form-item label="删除状态" prop="deleted">
          <el-input v-model="form.deleted" placeholder="请输入删除状态" />
        </el-form-item>
        <el-form-item label="订单编号" prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入订单编号" />
        </el-form-item>
        <el-form-item label="服务内容">
          <editor v-model="form.serviceContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="评价时间" prop="evaluateTime">
          <el-date-picker clearable
            v-model="form.evaluateTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择评价时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="评价星级(1-5)" prop="evaluateLevel">
          <el-input v-model="form.evaluateLevel" placeholder="请输入评价星级(1-5)" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Rescue">
import { listRescue, getRescue, delRescue, addRescue, updateRescue } from "@/api/system/rescue"

const { proxy } = getCurrentInstance()

const rescueList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderNo: null,
    serviceContent: null,
    evaluateLevel: null,
    evaluateTime: null
  },
  rules: {
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询救援任务列表 */
function getList() {
  loading.value = true
  listRescue(queryParams.value).then(response => {
    rescueList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    uid: null,
    typeId: null,
    position: null,
    endPosition: null,
    applyTime: null,
    name: null,
    phone: null,
    carNum: null,
    description: null,
    deleted: null,
    orderNo: null,
    serviceContent: null,
    evaluateTime: null,
    evaluateLevel: null
  }
  proxy.resetForm("rescueRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加救援任务"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getRescue(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改救援任务"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["rescueRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateRescue(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addRescue(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除救援任务编号为"' + _ids + '"的数据项？').then(function() {
    return delRescue(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/rescue/export', {
    ...queryParams.value
  }, `rescue_${new Date().getTime()}.xlsx`)
}

getList()
</script>

<style scoped>
.page-header {
  margin-bottom: 20px;
  padding: 20px;
  background: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.page-header h2 {
  margin: 0;
  color: #333;
  font-size: 18px;
  font-weight: 600;
}

.el-form {
  background: #fff;
  padding: 20px;
  border-radius: 4px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  margin-bottom: 20px;
}

.el-table {
  background: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.pagination-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 20px;
  padding: 20px;
  background: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.total-info {
  color: #666;
  font-size: 14px;
}
</style>
